home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / tptime.zip / TPTIMER.PAS < prev    next >
Pascal/Delphi Source File  |  1988-08-15  |  6KB  |  163 lines

  1. {$S-,R-,I-,V-,B-}
  2.  
  3. {*********************************************************}
  4. {*                   TPTIMER.PAS 2.00                    *}
  5. {*                by TurboPower Software                 *}
  6. {*********************************************************}
  7.  
  8. unit TpTimer;
  9.   {-Allows events to be timed with 1 microsecond resolution}
  10.  
  11. interface
  12.  
  13. procedure InitializeTimer;
  14.   {-Reprogram the timer chip to allow 1 microsecond resolution}
  15.  
  16. procedure RestoreTimer;
  17.   {-Restore the timer chip to its normal state}
  18.  
  19. function ReadTimer : LongInt;
  20.   {-Read the timer with 1 microsecond resolution}
  21.  
  22. function ElapsedTime(Start, Stop : LongInt) : Real;
  23.   {-Calculate time elapsed (in milliseconds) between Start and Stop}
  24.  
  25. function ElapsedTimeString(Start, Stop : LongInt) : string;
  26.   {-Return time elapsed (in milliseconds) between Start and Stop as a string}
  27.  
  28.   {==========================================================================}
  29.  
  30. implementation
  31.  
  32. const
  33.   TimerResolution = 1193181.667;
  34. var
  35.   SaveExitProc : Pointer;
  36.   Delta : LongInt;
  37.  
  38.   function Cardinal(L : LongInt) : Real;
  39.     {-Return the unsigned equivalent of L as a real}
  40.   begin                      {Cardinal}
  41.     if L < 0 then
  42.       Cardinal := 4294967296.0+L
  43.     else
  44.       Cardinal := L;
  45.   end;                       {Cardinal}
  46.  
  47.   function ElapsedTime(Start, Stop : LongInt) : Real;
  48.     {-Calculate time elapsed (in milliseconds) between Start and Stop}
  49.   begin                      {ElapsedTime}
  50.     ElapsedTime := 1000.0*Cardinal(Stop-(Start+Delta))/TimerResolution;
  51.   end;                       {ElapsedTime}
  52.  
  53.   function ElapsedTimeString(Start, Stop : LongInt) : string;
  54.     {-Return time elapsed (in milliseconds) between Start and Stop as a string}
  55.   var
  56.     R : Real;
  57.     S : string;
  58.   begin                      {ElapsedTimeString}
  59.     R := ElapsedTime(Start, Stop);
  60.     Str(R:0:3, S);
  61.     ElapsedTimeString := S;
  62.   end;                       {ElapsedTimeString}
  63.  
  64.   procedure InitializeTimer;
  65.     {-Reprogram the timer chip to allow 1 microsecond resolution}
  66.   begin                      {InitializeTimer}
  67.     {select timer mode 2, read/write channel 0}
  68.     Port[$43] := $34;        {00110100b}
  69.     inline($EB/$00);         {jmp short $+2 ;delay}
  70.     Port[$40] := $00;        {LSB = 0}
  71.     inline($EB/$00);         {jmp short $+2 ;delay}
  72.     Port[$40] := $00;        {MSB = 0}
  73.   end;                       {InitializeTimer}
  74.  
  75.   procedure RestoreTimer;
  76.     {-Restore the timer chip to its normal state}
  77.   begin                      {RestoreTimer}
  78.     {select timer mode 3, read/write channel 0}
  79.     Port[$43] := $36;        {00110110b}
  80.     inline($EB/$00);         {jmp short $+2 ;delay}
  81.     Port[$40] := $00;        {LSB = 0}
  82.     inline($EB/$00);         {jmp short $+2 ;delay}
  83.     Port[$40] := $00;        {MSB = 0}
  84.   end;                       {RestoreTimer}
  85.  
  86.   function ReadTimer : LongInt;
  87.     {-Read the timer with 1 microsecond resolution}
  88.   begin                      {ReadTimer}
  89.     inline(
  90.       $FA/                   {cli             ;Disable interrupts}
  91.       $BA/$20/$00/           {mov  dx,$20     ;Address PIC ocw3}
  92.       $B0/$0A/               {mov  al,$0A     ;Ask to read irr}
  93.       $EE/                   {out  dx,al}
  94.       $B0/$00/               {mov  al,$00     ;Latch timer 0}
  95.       $E6/$43/               {out  $43,al}
  96.       $EC/                   {in   al,dx      ;Read irr}
  97.       $89/$C7/               {mov  di,ax      ;Save it in DI}
  98.       $E4/$40/               {in   al,$40     ;Counter --> bx}
  99.       $88/$C3/               {mov  bl,al      ;LSB in BL}
  100.       $E4/$40/               {in   al,$40}
  101.       $88/$C7/               {mov  bh,al      ;MSB in BH}
  102.       $F7/$D3/               {not  bx         ;Need ascending counter}
  103.       $E4/$21/               {in   al,$21     ;Read PIC imr}
  104.       $89/$C6/               {mov  si,ax      ;Save it in SI}
  105.       $B0/$FF/               {mov  al,$0FF    ;Mask all interrupts}
  106.       $E6/$21/               {out  $21,al}
  107.       $B8/$40/$00/           {mov  ax,$40     ;read low word of time}
  108.       $8E/$C0/               {mov  es,ax      ;from BIOS data area}
  109.       $26/$8B/$16/$6C/$00/   {mov  dx,es:[$6C]}
  110.       $89/$F0/               {mov  ax,si      ;Restore imr from SI}
  111.       $E6/$21/               {out  $21,al}
  112.       $FB/                   {sti             ;Enable interrupts}
  113.       $89/$F8/               {mov  ax,di      ;Retrieve old irr}
  114.       $A8/$01/               {test al,$01     ;Counter hit 0?}
  115.       $74/$07/               {jz   done       ;Jump if not}
  116.       $81/$FB/$FF/$00/       {cmp  bx,$FF     ;Counter > $FF?}
  117.       $77/$01/               {ja   done       ;Done if so}
  118.       $42/                   {inc  dx         ;Else count int req.}
  119.       {done:}
  120.       $89/$5E/$FC/           {mov [bp-4],bx   ;set function result}
  121.       $89/$56/$FE);          {mov [bp-2],dx}
  122.   end;                       {ReadTimer}
  123.  
  124.   procedure Calibrate;
  125.     {-Calibrate the timer}
  126.   const
  127.     Reps = 1000;
  128.   var
  129.     I : Word;
  130.     L1, L2, Diff : LongInt;
  131.   begin                      {Calibrate}
  132.     Delta := MaxInt;
  133.     for I := 1 to Reps do begin
  134.       L1 := ReadTimer;
  135.       L2 := ReadTimer;
  136.       {use the minimum difference}
  137.       Diff := L2-L1;
  138.       if Diff < Delta then
  139.         Delta := Diff;
  140.     end;
  141.   end;                       {Calibrate}
  142.  
  143.   {$F+}
  144.   procedure OurExitProc;
  145.     {-Restore timer chip to its original state}
  146.   begin                      {OurExitProc}
  147.     ExitProc := SaveExitProc;
  148.     RestoreTimer;
  149.   end;                       {OurExitProc}
  150.   {$F-}
  151.  
  152. begin
  153.   {set up our exit handler}
  154.   SaveExitProc := ExitProc;
  155.   ExitProc := @OurExitProc;
  156.  
  157.   {reprogram the timer chip}
  158.   InitializeTimer;
  159.  
  160.   {adjust for speed of machine}
  161.   Calibrate;
  162. end.
  163.